<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">产品分类查询sku</p>
    </div>
    <div class="view-container">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="info"
            icon="el-icon-sort"
            size="mini"
            @click="toggleExpandAll"
          >展开/折叠</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-table v-if="refreshTable" v-loading="loading" :data="productlist" row-key="id"
            :default-expand-all="isExpandAll"
            :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          >
            <el-table-column prop="sortName" label="分类名称" width="180">
              <template slot-scope="scope">
                <span @click="handleItem(scope.row)" style="cursor:pointer">{{ scope.row.sortName }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="sortCode" label="分类编码" align="center"></el-table-column>
          </el-table>
        </el-col>
        <el-col :span="1">
          <div class="line"></div>
        </el-col>
        <el-col :span="15">
          <el-form :model="queryParams" ref="queryForm" :inline="true">
            <el-form-item prop="searchValue">
              <el-input
                v-model="queryParams.searchValue"
                placeholder="sku查询"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="list-search btn-success" color="#5ED4D4">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="list-search">重置</el-button>
            </el-form-item>
          </el-form>
          <el-table v-loading="loading" :data="searchProductList" @row-click="handleView" :default-sort="{prop: 'date', order: 'descending'}" border>
            <el-table-column label="序号" type="index" align="center" width="50">
              <template slot-scope="scope">
                <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
              </template>
            </el-table-column>
            <el-table-column label="SKU编码" align="center" prop="specificationCode" :show-overflow-tooltip="true" width="150" sortable />
            <el-table-column label="SKU名称" align="center" prop="specificationName" :show-overflow-tooltip="true" width="150" />
            <el-table-column label="助记码" align="center" prop="mnemonicCode"  :show-overflow-tooltip="true" width="100" />
            <el-table-column label="产品分类" align="center" prop="productClassification" :show-overflow-tooltip="true" width="150" />
            <!-- <el-table-column label="有效天数" align="center" prop="validDate" :show-overflow-tooltip="true" width="100" /> -->
            <!-- <el-table-column label="品牌" align="center" prop="brandId" :show-overflow-tooltip="true" width="100" /> -->
            <el-table-column label="单位" align="center" prop="unit" :show-overflow-tooltip="true" />
            <el-table-column label="是否启用扫码" align="center" prop="isSerial" width="120">
              <template slot-scope="scope">
                <div v-if="scope.row.isSerial == '0'" style="color:#52C41A">启用</div>
                <div v-if="scope.row.isSerial == '1'" style="color:#fd562c">禁用</div>
                <div v-else></div>
              </template>
            </el-table-column>
            <!-- <el-table-column label="是否启用序列号" align="center" prop="serialNumber" width="120">
              <template slot-scope="scope">
                <div v-if="scope.row.serialNumber == '0'" style="color:#13C2C2">启用</div>
                <div v-if="scope.row.serialNumber == '1'" style="color:#52C41A">禁用</div>
              </template>
            </el-table-column> -->
            <el-table-column label="审核状态" align="center" prop="approvalStatus">
              <template slot-scope="scope">
                <div v-if="scope.row.approvalStatus == 0" style="color:#13C2C2">草稿</div>
                <div v-if="scope.row.approvalStatus == 1" style="color:#13C2C2">待审核</div>
                <div v-if="scope.row.approvalStatus == 2" style="color:#52C41A">已审核</div>
                <div v-if="scope.row.approvalStatus == 3" style="color:#fd562c">拒绝</div>
              </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="productStatus">
              <template slot-scope="scope">
                <div v-if="scope.row.productStatus == 0" style="color:#fd562c">禁用</div>
                <div v-if="scope.row.productStatus == 1" style="color:#52C41A">正常</div>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="handleItem(queryParams)"
          />
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import { listProductList, searchSku } from "@/api/system/basedata/productlist/productlist";
import { listBrand } from "@/api/system/basedata/brand/brand"

export default {
  name: "productList",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 表格树数据
      productlist: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 产品列表
      searchProductList: [],
      // 查询参数
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sortId: '',
        isSerial: '',
        serialNumber: '',
        approvalStatus: '2',
        productStatus: '1',
        searchValue: undefined
      },
      id: '',
      type: '',
      supplier: '',
      productData: [],
      list: '',
      index: ''
    }
  },
  created() {
    this.getList()
    this.handleItem()
    this.id = this.$route.query.id
    this.type = this.$route.query.type
    this.supplier = this.$route.query.supplier
    this.list = this.$route.query.list
    this.productData = this.$route.query.productData
    this.index = this.$route.query.index
  },
  methods: {
    // 获取列表
    getList() {
      this.loading = true
      listProductList().then(res => {
        this.productlist = this.handleTree(res.rows, "id")
        this.loading = false
      })
    },
    // 转换产品分类数据结构 
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.sortName,
        children: node.children
      }
    },
    // 展开/折叠
    toggleExpandAll() {
      this.refreshTable = false
      this.isExpandAll = !this.isExpandAll
      this.$nextTick(() => {
        this.refreshTable = true
      })
    },
    // 根据分类查看产品sku
    handleItem(item){
      if(item !== undefined){
        this.queryParams.sortId = item.id
      }
      searchSku(this.addDateRange(this.queryParams)).then(res => {
        this.searchProductList = res.rows
        this.total = res.total
        this.getProductList()
        this.getBrand()
      })
    },
    // 获取产品分类
    getProductList(){
      listProductList().then(res => {
        this.searchProductList.forEach(item => {
          let arr = res.rows.find(e =>{
            return e.id == item.productClassification
          })
          if(arr != undefined){
            item.productClassification = arr.sortName
          }
        })
      })
    },
    // 获取品牌
    getBrand(){
      listBrand(this.addDateRange({status: 0})).then(res => {
        this.searchProductList.forEach(item => {
          let arr = res.rows.find(e => {
            return e.id == item.brandId
          })
          if(arr != undefined){
            item.brandId = arr.brandName
          }
        })
      })
    },
    // 搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1
      this.handleItem()
    },
    // 重置按钮
    resetQuery() {
      this.resetForm("queryForm")
      if(this.queryParams.sortId == ''){
        this.queryParams.sortId = ''
      }
      this.handleQuery()
    },
    // 查看详情
    handleView(row) {
      if(this.type == "1"){
        this.$tab.closeOpenPage({ path: "/purchaseOrder/add", query:{ list: row, supplier: this.supplier, productData: this.productData } })
      }else if(this.type == 'purchaseOrderEdit'){
        this.$tab.closeOpenPage({ path: "/purchaseOrder/edit", query:{ id: this.id, type: this.type, list: this.list, productData: this.productData, data: row } })
      }else if(this.type == 'salesOrderAdd'){
        this.$tab.closeOpenPage({ path: "/salesOrder/add", query:{ list: this.list, productData: this.productData, data: row, index: this.index } })
      }else if(this.type == 'salesOrderEdit'){
        this.$tab.closeOpenPage({ path: "/salesOrder/edit", query:{ id: this.id, list: this.list, productData: this.productData, data: row, index: this.index } })
      }
    }
  }
}
</script>
